package com.hss01248.dialog.bottomsheet;


import com.hss01248.dialog.ResourceTable;
import com.hss01248.dialog.ScreenUtil;
import com.hss01248.dialog.adapter.SuperLvHolder;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentState;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.app.Context;
import ohos.agp.utils.LayoutAlignment;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/10.
 */

public class BottomVpIndicatorHolder extends SuperLvHolder<List<List<BottomSheetBean>>> {

    public DirectionalLayout mLinearLayout;
    List<Component> dots;
    public BottomVpIndicatorHolder(Context context) {
        super(context);
    }

    @Override
    protected Component setRootView(Context context) {
        mLinearLayout = new DirectionalLayout(context);
        DirectionalLayout.LayoutConfig params = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_CONTENT,DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        params.setMarginBottom(ScreenUtil.dip2px(10));
        params.alignment = LayoutAlignment.HORIZONTAL_CENTER;
        mLinearLayout.setLayoutConfig(params);
        mLinearLayout.setOrientation(Component.HORIZONTAL);
        dots = new ArrayList<>();

        return mLinearLayout;
    }

    @Override
    protected void findViews() {

    }

    @Override
    protected int setLayoutRes() {
        return 0;
    }

    public void onPageSelected(int position){
        for (int i = 0; i < dots.size(); i++) {
            Component dot = dots.get(i);
            if(position ==i){
                dot.setEnabled(true);
            }else {
                dot.setEnabled(false);
            }
            dot.postLayout();
        }
    }

    @Override
    public void assingDatasAndEvents(Context context, List<List<BottomSheetBean>> bean) {
        if(bean ==null || bean.size() ==0){
            return;
        }
        int size = bean.size();
        int height = ScreenUtil.dip2px(5);
        int margin = ScreenUtil.dip2px(8);
        for (int i = 0; i < size; i++) {
            Component dot = new Component(context);
            DirectionalLayout.LayoutConfig params = new DirectionalLayout.LayoutConfig(height,height);
            StateElement se = new StateElement();
            se.addState(new int[]{ComponentState.COMPONENT_STATE_DISABLED}, new ShapeElement(context, ResourceTable.Graphic_dialogutil_dot_normal));
            se.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, new ShapeElement(context, ResourceTable.Graphic_dialogutil_dot_selected));
            dot.setBackground(se);
            if(i==size-1){
                params.setMargins(0,0,0,0);
            }else {
                params.setMargins(0,0,margin,0);
            }
            dot.setLayoutConfig(params);
            dot.setEnabled(false);
            dots.add(dot);
            mLinearLayout.addComponent(dot);
        }
    }
}
